# Maintainer: James Duley <jagduley gmail>
# Maintainer: Philippe Renon <philippe_renon yahoo.fr>

_realname=osgearth
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=3.3
pkgrel=4
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('spdx:LGPL-3.0-or-later WITH LGPL-3.0-linking-exception')
url="http://osgearth.org"
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-geos"
        #  "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-leveldb"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libzip"
         "${MINGW_PACKAGE_PREFIX}-poco"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-rocksdb"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
             "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug"
             "${MINGW_PACKAGE_PREFIX}-osgQt"
             "${MINGW_PACKAGE_PREFIX}-osgQt-debug"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
options=('staticlibs' 'strip')
_tag=${_realname}-${pkgver}
source=("${_realname}"::"git+https://github.com/gwaldron/osgearth.git#tag=${_tag}"
        "0001-calling-convention.patch")
sha256sums=('SKIP'
            'a17f6e6160b2b399a136090c6b714baeddf41bb3b92c804d571b329e19a32063')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}"

  apply_patch_with_msg \
    0001-calling-convention.patch
}

build() {
  export FREETYPE_DIR=${MINGW_PREFIX}

  for buildtype in {Release,Debug}; do
    [[ -d build-${MSYSTEM}-${buildtype} ]] && rm -rf build-${MSYSTEM}-${buildtype}
    mkdir -p build-${MSYSTEM}-${buildtype} && pushd build-${MSYSTEM}-${buildtype}
      [[ "${buildtype}" == "Release" ]] && {
        local _opts=""
      } || {
        local _opts=""
      }

      msg "Building ${buildtype} version..."

      MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
        -GNinja \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        -DCMAKE_BUILD_TYPE=${buildtype} \
        -DOSG_DIR="${MINGW_PREFIX}" \
        -DOSGEARTH_BUILD_EXAMPLES=OFF \
        -DOSGEARTH_BUILD_TESTS=OFF \
        -DOSGEARTH_BUILD_LEGACY_SPLAT_NODEKIT=ON \
        -DOSGEARTH_BUILD_LEVELDB_CACHE=ON \
        -DOSGEARTH_BUILD_ZIP_PLUGIN=ON \
        ${_opts} \
        ../${_realname}

      "${MINGW_PREFIX}"/bin/cmake.exe --build .
    popd
  done
}

package_osgearth() {
  depends+=("${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
            "${MINGW_PACKAGE_PREFIX}-osgQt")
  pkgdesc="A terrain rendering toolkit for OpenSceneGraph (mingw-w64)"

  cd build-${MSYSTEM}-Release
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  mv "${pkgdir}${MINGW_PREFIX}"/cmake "${pkgdir}${MINGW_PREFIX}"/lib/cmake

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/osgearth
  cp -rf "${srcdir}"/${_realname}/data "${pkgdir}${MINGW_PREFIX}"/share/osgearth

  install -Dm644 "${srcdir}/${_realname}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_osgearth-debug() {
  depends+=("${MINGW_PACKAGE_PREFIX}-osgearth"
            "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug"
            "${MINGW_PACKAGE_PREFIX}-osgQt-debug")
  options=('staticlibs' '!strip')
  pkgdesc="A terrain rendering toolkit for OpenSceneGraph (debug) (mingw-w64)"

  cd build-${MSYSTEM}-Debug
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  rm -rf "${pkgdir}${MINGW_PREFIX}"/cmake
  rm -rf "${pkgdir}${MINGW_PREFIX}"/include
  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig
  rm -rf "${pkgdir}${MINGW_PREFIX}"/share
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
